import 'package:flutter/material.dart';
import 'package:flutter_dk_form/src/widgets/show_text/widget_title.dart';
import 'widget_input.dart';

/// 带标题的多行输入框
/// 不需要controller
class MultipleInputWidget extends StatefulWidget {
  /// 标题
  final String title;

  /// 占位符
  final String placeholder;

  /// 最多输入文字
  final int maxLength;

  /// 是否显示底部最多显示字数的文本
  final bool showWaringText;

  /// 是否可编辑
  final bool readOnly;

  /// 内容区高度
  final double contentHeight;

  /// 按下return（完成键）键收起键盘
  final bool returnKeyDownKeyBoard;

  /// 输入改变
  ValueChanged<String> onChanged;
  final String initialValue;

  /// 标题样式，详见TitleTextStyle
  final TitleTextStyle titleTextStyle;

  /// [hasLine]：是否有下划线(false=标题自带下划线右侧无边距，ture=标题无下划线显示自带下划线左右边距16)
  final bool hasLine;

  MultipleInputWidget(
      {this.title = "",
      this.initialValue = '',
      this.placeholder = "",
      this.maxLength = 100,
      this.onChanged,
      this.showWaringText = true,
      this.readOnly = false,
      this.contentHeight = 65,
      this.returnKeyDownKeyBoard = true,
      this.titleTextStyle,
      this.hasLine = true});
  @override
  _InputStatefulWidgetState createState() => _InputStatefulWidgetState();
}

class _InputStatefulWidgetState extends State<MultipleInputWidget> {
  TextEditingController controller;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController();
    controller.text = widget.initialValue;
    controller.addListener(() {
      if (widget.onChanged != null) {
        widget.onChanged(controller.text);
      }
    });
  }

  final double countHeight = 20;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          TitleWidget(widget.title,
              hasLine: !widget.hasLine, titleTextStyle: widget.titleTextStyle),
          Visibility(
              visible: widget.hasLine,
              child: Divider(
                  height: 0.5,
                  indent: 16,
                  endIndent: 16,
                  color: Color(0xFFEBEBEB))),
          SizedBox(
            height: 16,
          ),
          InputWidget(
            placeholder: this.widget.placeholder,
            maxLength: this.widget.maxLength,
            showWaringText: this.widget.showWaringText,
            readOnly: this.widget.readOnly,
            contentHeight: this.widget.contentHeight,
            returnKeyDownKeyBoard: this.widget.returnKeyDownKeyBoard,
            controller: controller,
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
